﻿using System;
using System.Collections.Generic;

namespace Utility.Helper
{
    public static class HtmlHelper
    {
        public static string DateFormat(this object obj, string formate)
        {
            if (obj == null)
            {
                return "";
            }
            if (obj == DBNull.Value)
            {
                return "";
            }
            if (string.IsNullOrEmpty(formate))
            {
                if ((DateTime)obj < DateTime.Now.Date.AddDays(1) && (DateTime)obj > DateTime.Now.Date.AddDays(-1))
                {
                    return ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss");
                }
                return ((DateTime)obj).ToString("yyyy-MM-dd");
            }
            return Convert.ToDateTime(obj.ToString()).ToString(formate);
        }

        /// <summary>
        /// 数值保留有效小数
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="retain">保留小数点位数</param>
        /// <returns></returns>
        public static string ToFixed(this decimal value, int retain)
        {
            var str = "";
            if (retain == 0)
            {
                str = "{0:0}";
            }
            else
            {
                str = "{0:0.";
                for (int i = 0; i < retain; i++)
                {
                    str += "#";
                }
                str += "}";
            }
            return string.Format(str, value);
            //return Math.Round(value, retain).ToString();
        }

        /// <summary>
        /// 数组转字符串
        /// </summary>
        /// <param name="arry">Int数组</param>
        /// <returns>字符串</returns>
        public static string ToIn(this List<int> arry)
        {
            var str = "";
            foreach (var item in arry)
            {
                if (str != "")
                {
                    str += ",";
                }
                str += item;
            }
            return str;
        }

        /// <summary>
        /// 字符串转Int数组
        /// </summary>
        /// <param name="arryStr">字符串</param>
        /// <returns>数组</returns>
        public static List<int> ToArry(this string arryStr)
        {
            var arry = new List<int>();
            foreach (var item in arryStr.Split(','))
            {
                if (item != "")
                {
                    arry.Add(Convert.ToInt32(item));
                }
            }
            return arry;
        }

        /// <summary>
        /// 数值转换为千位符
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string QyFormat(this object _number, int bl = 6)
        {
            try
            {
                if (_number == null)
                {
                    return "";
                }
                if (_number == DBNull.Value)
                {
                    return "";
                }
                decimal number;
                try
                {
                    number = Convert.ToDecimal(_number);
                }
                catch (Exception)
                {
                    return "";
                }
                var str = "";
                //整数部分
                var zNumber = Math.Truncate(number).ToString();
                //小数部分

                
                var xNumber = Math.Truncate(((number - Math.Truncate(number)) * Convert.ToDecimal(Math.Pow(10, bl)))).ToString();
                if (xNumber.Length!= bl)
                {
                    for (int i = 0; i < bl-xNumber.Length; i++)
                    {
                        xNumber = "0" + xNumber;
                    }
                    
                }
                var arry = new List<string>();
                //整数部分
                while (true)
                {
                    if (zNumber.Length > 3)
                    {
                        arry.Add(zNumber.Substring(zNumber.Length - 3, 3));
                        zNumber = zNumber.Substring(0, zNumber.Length - 3);
                    }
                    else if (zNumber.Length == 0)
                    {
                        break;
                    }
                    else
                    {
                        arry.Add(zNumber);
                        zNumber = "";
                        break;
                    }
                }
                for (int i = arry.Count - 1; i >= 0; i--)
                {
                    if (str != "")
                    {
                        str += ",";
                    }
                    str += arry[i];
                }
                //去除小数点后面的0
                var _str = "";
                //采用反向算法
                for (int i = xNumber.Length - 1; i >= 0; i--)
                {
                    if (xNumber.Substring(i, 1) != "0")
                    {
                        //LENGTH 8 MAX_INDEX     7 , 8 = 7+1
                        //LENGTH 8 CURRENT_INDEX 6 , 7 = 6+1
                        //LENGTH 8 CURRENT_INDEX 5 , 6 = 7+1
                        _str = xNumber.Substring(0, i + 1);
                        break;
                    }
                }
                if (_str != "")
                {
                    str += "." + _str;
                }
                return str;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}